package com.example.service;

import com.example.dao.TsetRepository;

import com.example.entity.Tset;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class TsetService {
    @Resource
    private TsetRepository tsetRepository;

    public void save(Tset tset) {
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        tset.setCreateTime(now);
        tsetRepository.save(tset);
    }

    public void delete(Long id) {
        tsetRepository.deleteById(id);
    }

    public Tset findById(Long id) {
        return tsetRepository.findById(id).orElse(null);
    }

    public List<Tset> findAll() {
        return tsetRepository.findAll();
    }

    public Page<Tset> findPage(Integer pageNum, Integer pageSize, String name) {
        // 构建分页查询条件
        Sort sort = new Sort(Sort.Direction.DESC, "create_time");
        Pageable pageRequest = PageRequest.of(pageNum - 1, pageSize, sort);
        return tsetRepository.findByNameLike(name, pageRequest);
    }
}
